package pl.vanta.utils.gui.tables.renderers;

import java.text.NumberFormat;

import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;

public class NumberRenderer extends DefaultTableCellRenderer
{
	private static final long	serialVersionUID	= 1;

	private NumberFormat		numberFormat;

	public NumberRenderer(NumberFormat numberFormat)
	{
		this.numberFormat = numberFormat;
	}

	@Override
	public int getHorizontalAlignment()
	{
		return SwingConstants.RIGHT;
	}

	@Override
	protected void setValue(Object value)
	{
		if (value != null)
		{
			String newValue = value.toString();

			if (value instanceof Double || value instanceof Long)
			{
				newValue = this.numberFormat.format(value);
			}

			super.setValue(newValue);
		}
		else
		{
			super.setValue(value);
		}
	}
}